package Day15;

public class Main {

    public static void main(String[] args) {

        int[] left = new int[] {0, 7, 1, 6};
        int[] right = new int[] {1, 5, 0, 6};

        int i = findMinimum(4, left, right);
        System.out.println(i);
    }

    public static int findMinimum(int n, int[] left, int[] right) {
        //Sum用于比较左右哪个的总数最少, 少的用来多取, 多的用来只取一个
        int leftSum = 0;
        int rightSum = 0;
        //记录数组中除空最小值
        int leftMin = Integer.MAX_VALUE;
        int rightMin = Integer.MAX_VALUE;

        int ret = 0;


        for (int i = 0; i < n; i++) {
            int l = left[i];
            int r = right[i];

            if (l == 0) {
                //l==0, 则r全取
                ret += r;
            } else if (right[i] == 0) {
                //r==0, 则l全取
                ret += l;
            } else {
                //都不等于零, 累加入Sum
                rightSum += r;
                leftSum += l;

                //获得除了0那一列的最小值
                if (l < leftMin)
                    leftMin = l;
                if (r < rightMin)
                    rightMin = r;
            }
        }

        if (leftSum < rightSum) {
            ret += (leftSum - leftMin + 2);
        } else if (leftSum > rightSum){
            ret += (rightSum - rightMin + 2);
        } else {
            if (leftMin > rightMin) {
                ret += (leftSum - leftMin + 2);
            } else {
                ret += (rightSum - rightMin + 2);
            }
        }

        return ret;
    }

}
